<%@ page import="org.example.entity.Content" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    Content content = (Content) request.getAttribute("content");
    if (content == null) {
        response.sendRedirect(request.getContextPath() + "/content/list");
        return;
    }
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><%= content.getId().isEmpty() ? "添加" : "编辑" %>内容 - 内容评级平台</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        /* 样式与原文件一致，略去重复内容 */
        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --accent: #4cc9f0;
            --dark: #1d3557;
            --darker: #0d1b2a;
            --light: #011a32;
            --gray: #8d99ae;
            --success: #4caf50;
            --warning: #ff9800;
            --danger: #f44336;
            --card-bg: rgba(29, 53, 87, 0.8);
            --transition: all 0.3s ease;
        }
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        .btn {
            padding: 0.7rem 1.5rem;
            border-radius: 30px;
            font-weight: 600;
            font-size: 1rem;
            cursor: pointer;
            transition: var(--transition);
            border: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        .btn-primary {
            background: var(--primary);
            color: white;
        }
        .btn-primary:hover {
            background: var(--secondary);
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(67, 97, 238, 0.4);
        }
        .btn-outline {
            background: transparent;
            border: 2px solid var(--primary);
            color: var(--primary);
        }
        .btn-outline:hover {
            background: rgba(67, 97, 238, 0.1);
            transform: translateY(-2px);
        }
        .main-content {
            max-width: 800px;
            margin: 2rem auto;
            padding: 0 2rem;
        }
        .form-section {
            background: var(--card-bg);
            border-radius: 15px;
            padding: 2rem;
            margin-bottom: 3rem;
        }
        .section-title {
            font-size: 1.8rem;
            font-weight: 700;
            margin-bottom: 1.5rem;
            position: relative;
            padding-left: 1rem;
        }
        .section-title::before {
            content: "";
            position: absolute;
            left: 0;
            top: 0;
            height: 100%;
            width: 4px;
            background: var(--accent);
            border-radius: 2px;
        }
        .form-group {
            margin-bottom: 1.5rem;
        }
        .form-group label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .form-control {
            width: 100%;
            padding: 0.8rem 1rem;
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 8px;
            background: rgba(255, 255, 255, 0.05);
            color: var(--light);
            font-size: 1rem;
            transition: var(--transition);
        }
        .form-control:focus {
            outline: none;
            border-color: var(--accent);
            box-shadow: 0 0 0 2px rgba(76, 201, 240, 0.2);
        }
        textarea.form-control {
            min-height: 120px;
            resize: vertical;
        }
        .form-actions {
            display: flex;
            justify-content: flex-end;
            gap: 1rem;
            margin-top: 2rem;
        }
        .type-fields {
            display: none;
            margin-top: 15px;
            padding: 15px;
            background: rgba(255, 255, 255, 0.05);
            border-radius: 8px;
            border-left: 3px solid var(--accent);
        }
        .type-active {
            display: block;
        }
        .cover-preview {
            max-width: 200px;
            max-height: 300px;
            margin-top: 10px;
            border-radius: 4px;
            border: 1px solid rgba(255, 255, 255, 0.1);
        }

    </style>
</head>
<body>
<!-- 页头、页脚与原文件一致 -->
<jsp:include page="header.jsp" />
<div class="main-content">
    <section class="form-section">
        <h1 class="section-title"><i class="fas fa-edit"></i> <%= content.getId().isEmpty() ? "添加" : "编辑" %>内容</h1>
        <form action="${pageContext.request.contextPath}/content/<%= content.getId().isEmpty() ? "add" : "update" %>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="id" value="<%= content.getId() %>">
            <div class="form-group">
                <label for="title"><i class="fas fa-heading"></i> 标题</label>
                <input type="text" id="title" name="title" class="form-control" value="<%= content.getTitle() %>" required>
            </div>
            <div class="form-group">
                <label for="type"><i class="fas fa-tag"></i> 类型</label>
                <select id="type" name="type" class="form-control" required>
                    <option value="movie" <%= "movie".equals(content.getType()) ? "selected" : "" %>>电影</option>
                    <option value="book" <%= "book".equals(content.getType()) ? "selected" : "" %>>图书</option>
                    <option value="music" <%= "music".equals(content.getType()) ? "selected" : "" %>>音乐</option>
                </select>
            </div>
            <div class="form-group">
                <label for="cover"><i class="fas fa-image"></i> 封面图片</label>
                <input type="file" id="cover" name="cover" class="form-control" accept="image/*">
                <% if (content.getCoverUrl() != null && !content.getCoverUrl().isEmpty()) { %>
                <div style="margin-top: 10px;">
                    <span>当前封面:</span>
                    <img src="<%= content.getCoverUrl() %>" class="cover-preview" style="max-width: 200px; max-height: 300px; margin-top: 5px;">
                    <input type="hidden" name="existingCover" value="<%= content.getCoverUrl() %>">
                </div>
                <% } %>
            </div>
            <div class="form-group">
                <label for="description"><i class="fas fa-align-left"></i> 描述</label>
                <textarea id="description" name="description" class="form-control" required><%= content.getDescription() %></textarea>
            </div>
            <!-- 类型特定字段 -->
            <div id="movie-fields" class="type-fields <%= "movie".equals(content.getType()) ? "type-active" : "" %>">
                <div class="form-group">
                    <label for="director"><i class="fas fa-user-tie"></i> 导演</label>
                    <input type="text" id="director" name="director" class="form-control" value="<%= content.getDirector() != null ? content.getDirector() : "" %>">
                </div>
                <div class="form-group">
                    <label for="actors"><i class="fas fa-users"></i> 演员</label>
                    <input type="text" id="actors" name="actors" class="form-control" value="<%= content.getActors() != null ? content.getActors() : "" %>">
                </div>
            </div>
            <div id="book-fields" class="type-fields <%= "book".equals(content.getType()) ? "type-active" : "" %>">
                <div class="form-group">
                    <label for="author"><i class="fas fa-user-edit"></i> 作者</label>
                    <input type="text" id="author" name="author" class="form-control" value="<%= content.getAuthor() != null ? content.getAuthor() : "" %>">
                </div>
                <div class="form-group">
                    <label for="publisher"><i class="fas fa-building"></i> 出版社</label>
                    <input type="text" id="publisher" name="publisher" class="form-control" value="<%= content.getPublisher() != null ? content.getPublisher() : "" %>">
                </div>
            </div>
            <div id="music-fields" class="type-fields <%= "music".equals(content.getType()) ? "type-active" : "" %>">
                <div class="form-group">
                    <label for="artist"><i class="fas fa-user"></i> 艺术家</label>
                    <input type="text" id="artist" name="artist" class="form-control" value="<%= content.getArtist() != null ? content.getArtist() : "" %>">
                </div>
                <div class="form-group">
                    <label for="album"><i class="fas fa-compact-disc"></i> 专辑</label>
                    <input type="text" id="album" name="album" class="form-control" value="<%= content.getAlbum() != null ? content.getAlbum() : "" %>">
                </div>
            </div>
            <div class="form-group">
                <label for="status"><i class="fas fa-check-circle"></i> 状态</label>
                <select id="status" name="status" class="form-control">
                    <option value="draft" <%= "draft".equals(content.getStatus()) ? "selected" : "" %>>草稿</option>
                    <option value="published" <%= "published".equals(content.getStatus()) ? "selected" : "" %>>已发布</option>
                    <option value="archived" <%= "archived".equals(content.getStatus()) ? "selected" : "" %>>已归档</option>
                </select>
            </div>
            <div class="form-actions">
                <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> 保存</button>
                <a href="content-management.jsp" class="btn btn-outline"><i class="fas fa-times"></i> 取消</a>
            </div>
        </form>
    </section>
</div>
<jsp:include page="footer.jsp" />
<script>
    // 根据类型显示/隐藏特定字段
    document.getElementById('type').addEventListener('change', function() {
        document.querySelectorAll('.type-fields').forEach(el => el.classList.remove('type-active'));
        const selectedType = this.value;
        document.getElementById(selectedType + '-fields').classList.add('type-active');
    });
    // 封面图片预览
    document.getElementById('cover').addEventListener('change', function(e) {
        const file = e.target.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function(event) {
                let previewContainer = document.getElementById('cover-preview-container');
                if (!previewContainer) {
                    previewContainer = document.createElement('div');
                    previewContainer.id = 'cover-preview-container';
                    previewContainer.style.marginTop = '10px';
                    previewContainer.innerHTML = '<span>新封面预览:</span><img class="cover-preview" style="max-width: 200px; max-height: 300px; margin-top: 5px;">';
                    e.target.parentNode.appendChild(previewContainer);
                }
                const preview = previewContainer.querySelector('.cover-preview');
                preview.src = event.target.result;
            };
            reader.readAsDataURL(file);
        }
    });
    // 显示操作结果提示
    <% if (request.getAttribute("actionResult") != null) {
        String actionResult = (String) request.getAttribute("actionResult");
        if ("addSuccess".equals(actionResult)) { %>
    alert("内容添加成功！");
    <% } else if ("updateSuccess".equals(actionResult)) { %>
    alert("内容更新成功！");
    <% } %>
    <% } %>
</script>
</body>
</html>